<?php
/**
 * CakeCMS - 附件管理
 * 功能模块:
 *
 * @author zzdboy
 * @version 1.0
 */
App::import ( 'Vendor', 'Uploader' );
class AttachmentController extends AppController {
	public $components = array (
			'CheckUserPermission' 
	);
	
	/**
	 * 初始化执行
	 *
	 * @see AppController::beforeFilter()
	 */
	public function beforeFilter() {
		parent::beforeFilter ();
		
		// 判断是否登录
		$this->CheckUserPermission->checkLogin ( $this );
		
		$this->set ( 'title_for_layout', 'CakeCMS附件管理' );
	}
	public function index() {
		$this->set ( 'title_for_layout', '附件上传' );
		
		// 加载js文件
		$this->set ( 'scriptList', array (
				'ajaxfileupload.js' 
		) );
		
		$type = $this->params ['type'];
		if ($type == 'content') {
			// 内容图片
			$this->set ( 'url', 'upload_title_img' );
		} elseif ($type == 'goods') {
			// 商品图片
			$this->set ( 'url', 'upload_goods_img' );
		}
		$id = $this->params ['id'];
		$this->set ( 'type', $type );
		$this->set ( 'id', $id );
	}
	/**
	 * 图库
	 */
	public function albumload() {
		
		// 加载css文件
		$this->set ( 'cssList', array (
				'calendar/jscal2.css',
				'calendar/border-radius.css',
				'calendar/win2k.css' 
		) );
		
		// 加载js文件
		$this->set ( 'scriptList', array (
				'calendar/calendar.js',
				'calendar/lang/en.js',
				'jquery.cookie.js' 
		) );
		
		$id = $this->params ['pass'] [0];
		
		$this->set ( 'id', $id );
	}
	/**
	 * 目录浏览
	 */
	public function albumdir() {
		
		// 加载css文件
		$this->set ( 'cssList', array (
				'calendar/jscal2.css',
				'calendar/border-radius.css',
				'calendar/win2k.css' 
		) );
		
		// 加载js文件
		$this->set ( 'scriptList', array (
				'calendar/calendar.js',
				'calendar/lang/en.js',
				'jquery.cookie.js' 
		) );
		
		$id = $this->params ['pass'] [0];
		
		$this->set ( 'id', $id );
	}
	/**
	 * 商品图片
	 */
	public function upload_goods_img() {
		$this->autoRender = false;
		
		$error = "";
		$msg = "";
		
		if (! empty ( $_FILES ['image'] ['error'] )) {
			switch ($_FILES ['image'] ['error']) {
				
				case '1' :
					$error = '上传的文件超过 php.ini限制!';
					break;
				case '2' :
					$error = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
					break;
				case '3' :
					$error = 'The uploaded file was only partially uploaded';
					break;
				case '4' :
					$error = '没有上传文件!';
					break;
				case '6' :
					$error = '未了找到临时文件夹!';
					break;
				case '7' :
					$error = '未能写文件到磁盘!';
					break;
				case '8' :
					$error = 'File upload stopped by extension';
					break;
				case '999' :
				default :
					$error = 'No error code avaiable';
			}
		} elseif (empty ( $_FILES ['image'] ['tmp_name'] ) || $_FILES ['image'] ['tmp_name'] == 'none') {
			$error = '请选择上传的文件..';
		} elseif ($_FILES ['image'] ['type'] != 'image/jpeg') {
			$error = '请正确选择图片类型..';
		} else {
			$this->Uploader = new Uploader ();
			// 内容
			$this->Uploader->setup ( array (
					'tempDir' => CAKE_CORE_INCLUDE_PATH . DS . 'www' . DS . 'tmp' . DS,
					'baseDir' => CAKE_CORE_INCLUDE_PATH . DS . 'www' . DS . 'webroot' . DS,
					'uploadDir' => 'files' . DS . 'goods' . DS . date ( "Ymd", time () ) 
			) );
			// 上传图片
			$image = $this->Uploader->upload ( 'image', array (
					'name' => date ( "YmdHis", time () ),
					'overwrite' => true 
			) );
			
			$image ['path'] = str_replace ( "\\", "/", $image ['path'] );
			
			if (! $image) {
				$error = '图片上传错误..';
			} else {
				// 缩图
				$this->Uploader->resize ( array (
						'width' => 80,
						'height' => 80,
						'append' => '_' . 80,
						'quality' => 100
				) );
				
				$this->Uploader->resize ( array (
						'width' => 100,
						'height' => 100,
						'append' => '_' . 100,
						'quality' => 100
				) );
				
				$this->Uploader->resize ( array (
						'width' => 200,
						'height' => 200,
						'append' => '_' . 200,
						'quality' => 100 
				) );
				
				$this->Uploader->resize ( array (
						'width' => 460,
						'append' => '_' . 460,
						'quality' => 100
				) );
				
				$images = explode ( '.', $image ['path'] );
				$image_small = $images [0] . '_' . 200 . '.' . $images [1];
				$msg = str_replace ( "\\", "/", $image_small );
			}
		}
		
		echo "{";
		echo "error: '" . $error . "',\n";
		echo "msg: '" . $msg . "'\n";
		echo "}";
		exit ();
	}
	/**
	 * 上传内容标题图片
	 */
	public function upload_title_img() {
		$this->autoRender = false;
		
		$error = "";
		$msg = "";
		
		if (! empty ( $_FILES ['image'] ['error'] )) {
			switch ($_FILES ['image'] ['error']) {
				
				case '1' :
					$error = '上传的文件超过 php.ini限制!';
					break;
				case '2' :
					$error = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
					break;
				case '3' :
					$error = 'The uploaded file was only partially uploaded';
					break;
				case '4' :
					$error = '没有上传文件!';
					break;
				case '6' :
					$error = '未了找到临时文件夹!';
					break;
				case '7' :
					$error = '未能写文件到磁盘!';
					break;
				case '8' :
					$error = 'File upload stopped by extension';
					break;
				case '999' :
				default :
					$error = 'No error code avaiable';
			}
		} elseif (empty ( $_FILES ['image'] ['tmp_name'] ) || $_FILES ['image'] ['tmp_name'] == 'none') {
			$error = '请选择上传的文件..';
		} elseif ($_FILES ['image'] ['type'] != 'image/jpeg') {
			$error = '请正确选择图片类型..';
		} else {
			$this->Uploader = new Uploader ();
			// 内容
			$this->Uploader->setup ( array (
					'tempDir' => CAKE_CORE_INCLUDE_PATH . DS . 'www' . DS . 'tmp' . DS,
					'baseDir' => CAKE_CORE_INCLUDE_PATH . DS . 'www' . DS . 'webroot' . DS,
					'uploadDir' => 'files' . DS . 'content' . DS . date ( "Ymd", time () ) 
			) );
			// 上传图片
			$image = $this->Uploader->upload ( 'image', array (
					'name' => date ( "YmdHis", time () ),
					'overwrite' => true 
			) );
			
			$image ['path'] = str_replace ( "\\", "/", $image ['path'] );
			
			if (! $image) {
				$error = '图片上传错误..';
			} else {
				// 缩图
				$this->Uploader->resize ( array (
						'width' => 100,
						'height' => 70,
						'append' => '_' . 100,
						'quality' => 100 
				) );
				
				$images = explode ( '.', $image ['path'] );
				$image_small = $images [0] . '_' . 100 . '.' . $images [1];
				$msg = str_replace ( "\\", "/", $image_small );
			}
		}
		
		echo "{";
		echo "error: '" . $error . "',\n";
		echo "msg: '" . $msg . "'\n";
		echo "}";
		exit ();
	}
}
?>